package com.ctshk.rpc.order.bus.service.impl;

import java.time.LocalDateTime;
import java.util.Objects;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OrderCollectionStatus;
import com.ctshk.common.enums.SystemBusinessType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.service.support.BaseService;
import com.ctshk.rpc.finance.req.bill.BillOperateRefundReq;
import com.ctshk.rpc.finance.service.IFinanceBillService;
import com.ctshk.rpc.order.bus.entity.BusOrderCollection;
import com.ctshk.rpc.order.bus.mapper.BusOrderCollectionMapper;
import com.ctshk.rpc.order.bus.service.IBusOrderCollectionService;

/**
 * <p>
 * 巴士收款明细 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-02-24
 */
@DubboService
public class BusOrderCollectionServiceImpl extends ServiceImpl<BusOrderCollectionMapper, BusOrderCollection> implements IBusOrderCollectionService, BaseService {

    @Autowired
    private BusOrderCollectionMapper busOrderCollectionMapper;

    @DubboReference
    IFinanceBillService iFinanceBillService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result reconfirm(Long id, Long userId) {
        BusOrderCollection busOrderCollection = busOrderCollectionMapper.selectById(id);
        if (busOrderCollection == null) {
            //收款记录不存在
            throw new BusinessException(SystemError.BUS_ORDER_9008);
        }
        if (busOrderCollection.getStatus() == null || busOrderCollection.getStatus() != OrderCollectionStatus.REJECT.getCode()) {
            // 收款记录的状态必须是驳回，才能再次确认
            throw new BusinessException(SystemError.BUS_ORDER_9014);
        }
        busOrderCollection.setStatus(OrderCollectionStatus.UN_REJECT.getCode());
        busOrderCollection.setRejectInfo("");
        busOrderCollection.setModifiedId(userId);
        busOrderCollection.setGmtModified(LocalDateTime.now());
        busOrderCollectionMapper.updateById(busOrderCollection);
        
        //再次确认收款对接财务中心
        reconfirmToFinance(busOrderCollection, userId);
        
        return Result.success();
    }
    
    /**
     * 驳回收款(财务-内部调用)
     * @param id
     * @return
     */
    @Override
    public Result<Void> rejectPayment(String collectionNumber, Long userId, String msg) {
    	validateNotBlank(collectionNumber);
    	
    	BusOrderCollection entity = busOrderCollectionMapper.assertExistSelectOneByKey("collection_number", collectionNumber);
    	if (Objects.equals(entity.getStatus(), OrderCollectionStatus.REJECT.getCode())) {
            // 收款记录的状态必须不等于“已驳回”
            return Result.failed(SystemError.AIREXPRESS_ORDER_60015);
        }
    	
    	fillUpdateEntityUserId(entity, userId);
    	entity.setStatus(OrderCollectionStatus.REJECT.getCode());
    	entity.setRejectInfo(msg);
    	
    	busOrderCollectionMapper.updateById(entity);
    	
    	return newResult();
    }
    
    
    
    
    

    /**
     * 再次确认收款对接财务中心
     */
    private void reconfirmToFinance(BusOrderCollection collectionEntity, Long userId) {
    	BillOperateRefundReq callReq = new BillOperateRefundReq();
    	callReq.setBillNumber(collectionEntity.getCollectionNumber());
    	callReq.setBusinessType(SystemBusinessType.BUS_TICKET.getCode());
    	callReq.setUserId(userId);
        iFinanceBillService.reConfirm(callReq);
	}
}
